
package com.wyzz.hr.bo.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IEmpRewardBo;
import com.wyzz.hr.dao.IEmpRewardDao;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empreward;
import com.wyzz.hr.util.Pager;


public class EmpRewardBoImpl implements IEmpRewardBo {

	public EmpRewardBoImpl() {
	}

	public List findRewardByEmpNo(String empNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empreward.class);
		detachedCriteria.setFetchMode(Empreward.PROP_EMPLOYEE, FetchMode.JOIN);
		detachedCriteria
				.setFetchMode(Empreward.PROP_DEPARTMENT, FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empreward.PROP_ER_PB_NO, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
				Empreward.PROP_EMPLOYEE).append(".").append(Employee.PROP_ID)
				.toString(), empNo));
		detachedCriteria.addOrder(Order.desc(Empreward.PROP_ER_EXE_DATE));
		List result = empRewardDao.findByCriteria(detachedCriteria);
		return result;
	}

	public List findReward(DetachedCriteria dc, Pager page,
			String searchOrExport) {
		if (page == null || "export".equals(searchOrExport)) {
			return empRewardDao.findByCriteria(dc);
		} else {
			page.splitPage(dc);
			return empRewardDao.findByCriteria(dc, page.getPageSize(), page
					.getCurrentPage());
		}
	}

	public Empreward getById(String rewardId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empreward.class);
		detachedCriteria.setFetchMode(Empreward.PROP_EMPLOYEE, FetchMode.JOIN);
		detachedCriteria
				.setFetchMode(Empreward.PROP_DEPARTMENT, FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empreward.PROP_ER_PB_NO, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq(Empreward.PROP_ER_ID, rewardId));
		List result = empRewardDao.findByCriteria(detachedCriteria);
		if (result.size() == 0)
			return null;
		else
			return (Empreward) result.get(0);
	}

	public boolean insertEmp(Empreward empReward) {
		try {
			empRewardDao.saveObject(empReward);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String insert(Empreward empReward) {
		empRewardDao.saveObject(empReward);
		return empReward.getErId();
	}

	public void delete(String etfId) {
		String hql = (new StringBuilder()).append(
				"delete from Empreward where erId='").append(etfId).append("'")
				.toString();
		empRewardDao.exeHql(hql);
	}

	public void update(Empreward empReward, String rewardId) {
		Empreward oldReward = getById(rewardId);
		oldReward.setErContent(empReward.getErContent());
		oldReward.setErExeDate(empReward.getErExeDate());
		oldReward.setErType(empReward.getErType());
		oldReward.setErForm(empReward.getErForm());
		oldReward.setErLastChangeBy(empReward.getErLastChangeBy());
		oldReward.setErLastChangeTime(new Date());
		empRewardDao.saveObject(oldReward);
	}

	public IEmpRewardDao getEmpRewardDao() {
		return empRewardDao;
	}

	public void setEmpRewardDao(IEmpRewardDao empRewardDao) {
		this.empRewardDao = empRewardDao;
	}

	IEmpRewardDao empRewardDao;
}
